<template>
  <div>
    <div style="margin-bottom: 12px;display: flex;">
      <el-button type="primary" @click="add">新增运费</el-button>
    </div>
    <el-table :data="table_body" border max-height="300">
      <el-table-column prop="effectiveFrom" label="生效时间" />
      <el-table-column prop="effectiveTo" label="失效时间" />
      <el-table-column prop="updatedBy" label="最后操作人" />
      <el-table-column prop="updatedAt" label="最后更新时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right; padding-top: 10px;">
      <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" @size-change="onPageSizeChange" @current-change="onPageChange" />
    </div>

    <el-dialog :visible.sync="openBasePriceNewModal" width="60%" :close-on-click-modal="false" :append-to-body="true" @opened="opened">
      <BasePriceNew ref="basePriceNew" @basePriceOk="submit" />
      <div slot="footer">
        <el-button style="margin-right: 8px" @click="openBasePriceNewModal = false">取消</el-button>
        <el-button type="primary" @click="basePriceValidate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
import BasePriceNew from './base_price_new'
export default {
  components: {
    BasePriceNew
  },
  data() {
    return {
      openBasePriceNewModal: false,
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      table_header: [
        { key: 'effectiveFrom', title: '生效时间' },
        { key: 'effectiveTo', title: '失效时间' },
        { key: 'updatedBy', title: '最后操作人' },
        { key: 'updatedAt', title: '最后更新时间' },
        { slot: 'action', title: '操作' }
      ],
      table_body: [],
      queryData: {
        channelId: '',
        pageSize: 50,
        currentPage: 1
      },
      formData: {
        channelId: '',
        dfId: '',
        effectiveFrom: '',
        zoneId: '',
        weightId: '',
        basePrice: [],
        zoneDirection: ''
      },
      addPrice: true
    }
  },
  computed: {
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
  },
  methods: {
    opened() {
      if (this.addPrice) {
        this.$refs.basePriceNew.clean()
      } else {
        this.$refs.basePriceNew.edit({ weightId: this.formData.weightId, zoneId: this.formData.zoneId, dfId: this.formData.dfId, zoneDirection: this.formData.zoneDirection })
      }
    },
    basePriceValidate() {
      this.$refs.basePriceNew.doValidate()
    },
    add() {
      this.addPrice = true
      this.openBasePriceNewModal = true
    },
    list() {
      request({
        url: `/channel/base-price`,
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          this.pageTotal = res.data.total
          this.table_body = res.data.records
        }
      })
    },
    edit(row) {
      this.addPrice = false
      this.formData.dfId = row.dfId
      this.formData.weightId = row.weightId
      this.formData.zoneId = row.zoneId
      this.formData.zoneDirection = row.zoneDirection

      this.openBasePriceNewModal = true
    },
    submit(data) {
      copyProperty(this.formData, data)
      this.formData.channelId = this.queryData.channelId
      if (this.formData.effectiveFrom) {
        this.formData.dfId = ''
      }
      request({
        url: 'channel/add-base-price',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openDrawer = false
          this.list()
          this.openBasePriceNewModal = false
          this.$refs.basePriceNew.clean()
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
